/*
    Interface Program for the Linux Driver for 
      Enterpoint's Raggedstone1 FPGA PCI Board
    This demo driver allows access to the Board's 7segment displays.

    Written by Manuel Bessler <m.bessler AT gmx.net>
    Based on examples from the Linux Kernel Module Programming Guide
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or 
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <fcntl.h>      /* open */ 
#include <unistd.h>     /* exit */
#include <sys/ioctl.h>  /* ioctl */
#include <netinet/in.h>
#include <stdlib.h>

#define MAJOR_NUM 100
#define IOCTL_SETDPY _IOR(MAJOR_NUM, 0, short int)
#define IOCTL_SETGPIO _IOR(MAJOR_NUM, 2, short int)
#define DEVICE_NAME "/dev/lcpd-scope"


ioctl_setdpy(int file_desc, short int data)
{
	int ret_val;

	ret_val = ioctl(file_desc, IOCTL_SETDPY, data);

	if (ret_val < 0) 
	{
		printf ("ioctl_set_msg failed:%d\n", ret_val);
		exit(-1);
	}
}

main(int argc, char ** argv)
{
	int file_desc, ret_val;
	char *msg = "Message passed by ioctl\n";
	short int val = 0xDEAD;
	int Buffer[2000];
	file_desc = open(DEVICE_NAME, 0);
	if (file_desc < 0) 
	{
		printf ("Can't open device file: %s\n", DEVICE_NAME);
		exit(-1);
	}

	if(argc >= 2 )
	{
		sscanf(argv[1], "0x%hx", &val);
		val = htons(val);
	}
	else val = htons(val);
	ioctl_setdpy(file_desc, val);

	read (file_desc,Buffer,2000);
	int i;
	for (i=0;i<2000;i++)
	{
		printf("%d ",Buffer[i]);
	}
	//ioctl(file_desc, IOCTL_SETGPIO, 0);
	close(file_desc); 
}
